<?php
namespace app\home\model;
use think\Model;

class CourseSubjectModel extends Model
{
	protected $table  = 'qm_course_subject';


	/**
	*
	*查找某一部分课程的所有学科ID
	**/
	public function getCourseSubjectId($courseCount)
	{
		$list = $this->where('course_id', 'in', $courseCount)->group('id')->column('subject_id');

		if(!empty($list))
		{
			$list = array_unique($list);
			sort($list);
		}
		
		return $list;
	}


	/**
	*
	*查找某一部分课程+某一学科的信息
	**/
	public function getCourseWhereCourseCountsAndSujectId($subject_id_count, $subject_id, $page = 1, $field = '*')
	{
		
		$keys = (($page - 1) * 5);
		$limit = 5;


		$date = date('Y-m-d H:i:s');
		$where = [
			['c.status', '=', 1],
			['c.start_time', '<=', $date],
			['c.end_time', '>=', $date],
		];
		$list = $this->alias('cs')->where('cs.course_id', 'in', $subject_id_count)
				->where('cs.subject_id', '=', $subject_id)
				->where($where)
				->join('qm_course c', 'cs.course_id = c.id')->order('c.sort DESC')->limit($keys, $limit)->select();

		if(!empty($list))
		{
			$list = $list->toArray();
		}
		
		return $list;
	}



	/**
	*
	*查找某一学科的课程信息
	**/
	public function getCourseWhereSujectId($subject_id, $page = 1, $field = '*')
	{
		$keys = (($page - 1) * 5);
		$limit = 5;

		$date = date('Y-m-d H:i:s');
		$where = [
			['c.status', '=', 1],
			['c.start_time', '<=', $date],
			['c.end_time', '>=', $date],
		];
		
		$list = $this->alias('cs')->where('cs.subject_id', '=', $subject_id)
					->where($where)
					->join('qm_course c', 'cs.course_id = c.id')->order('c.sort DESC')->limit($keys, $limit)->select();

		if(!empty($list))
		{
			$list = $list->toArray();
		}
		
		return $list;
	}
}
